package cn.test.client;

import cn.test.domain.User;
import cn.test.utils.ByteBufUtil;
import cn.test.utils.ByteHexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.EmptyByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

/***
 *
 *
 * @Author: SJie
 * Date: 2019/12/19 下午5:44
 * Description:
 *
 */

public class ClientHandler1 extends SimpleChannelInboundHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object o) throws Exception {
        byte[] data= ByteBufUtil.ByteBufToBytes(o);
        for (byte datum : data) {
            System.out.println(ByteHexUtil.bytesToHexStr(data));
        }
        System.out.println("channelRead1");
//        ctx.channel().writeAndFlush(new User());
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        ctx.channel().writeAndFlush(new User());
//        ctx.channel().writeAndFlush(new User());
//        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer();
//        byte[] b = {100,101,102,103};
//        byteBuf.writeBytes(b);
//        ctx.channel().writeAndFlush(byteBuf);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
        super.channelInactive(ctx);

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
